package com.yc.autouser.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.autouser.entity.SysUser;
import com.yc.autouser.entity.vo.LoginUserVo;
import com.yc.autouser.entity.vo.RegisterUserVo;
import com.yc.autouser.mapper.SysUserMapper;
import com.yc.autouser.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.common.res.ResultClass;
import com.yc.common.utils.JwtUtils;
import com.yc.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname SysUserController
 * @Description
 * @Version 1.0.0
 * @Date 2022-07-15 04:33:38
 * @Created by Mikechen
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private RedisUtils redisUtils;
    @Autowired
    private AuthenticationManager authenticationManager;



    public ResultClass registerUser(RegisterUserVo registerUserVo) {
        //1.邮箱验证码验证
        String currentEmailCode = (String) redisUtils.get(registerUserVo.getEmail());
        if (currentEmailCode == null || currentEmailCode.length() == 0 || !registerUserVo.getVerificationCode().equals(currentEmailCode)) {
            return ResultClass.fail(700, "验证码错误");
        }
        //2验证用户是否已存在
        QueryWrapper queryWrapper = new QueryWrapper();
        SysUser insertUser=new SysUser();//插入对象
        if (registerUserVo.getType().equals("1")) {
            queryWrapper.eq("username", registerUserVo.getPhone());
            insertUser.setIdentityType("1");
            insertUser.setUsername(registerUserVo.getPhone());
        } else {
            queryWrapper.eq("username", registerUserVo.getEmail());
            insertUser.setIdentityType("2");
            insertUser.setUsername(registerUserVo.getEmail());
        }
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        if (sysUser != null) {
            return ResultClass.fail(700, "用户名已存在");
        }
        insertUser.setPassword(registerUserVo.getPassword());
        insertUser.setState(1);//初始状态为正常
        insertUser.setCreateTime(new Date());
        insertUser.setUpdateTime(new Date());//初始修改时间为创建时间

        sysUserMapper.insert(insertUser);
        return ResultClass.success();

    }

    @Override
    public ResultClass loginUser(SysUser sysUser) {
        //authenticationManager进行用户认证
        UsernamePasswordAuthenticationToken authenticationToken=new UsernamePasswordAuthenticationToken(sysUser.getUsername(),sysUser.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //如果认证失败返回错误
        if (Objects.isNull(authenticate)){
            return ResultClass.fail(700,"密码错误");

        }
        //生成token
        LoginUserVo loginUserVo = (LoginUserVo) authenticate.getPrincipal();
        String token=JwtUtils.createToken(loginUserVo.getUsername());

        //认证成功生产token存入redis,key=username,value=sysuser
        redisUtils.put("login:"+loginUserVo.getUsername(),loginUserVo);
        Map map=new HashMap();
        System.out.println(token);
        map.put("token",token);
        System.out.println(map);
        System.out.println(ResultClass.success(map));
        return ResultClass.success(map);
    }
}
